/**
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License version 2 as published by the Free Software Foundation.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
  */

#include "kickass_config.h"
#include "kickass.h"

#include <QtGui/QBoxLayout>
#include <QtGui/QFormLayout>
#include <QtGui/QCheckBox>

#include <klocale.h>
#include <kpluginfactory.h>
#include <kpluginloader.h>
#include <klineedit.h>
#include <QtGui/QLabel>
#include <QtGui/QGroupBox>
#include <QtGui/QPushButton>
#include <kconfiggroup.h>
//#include <kfilewidget.h>
#include <kfiledialog.h>

//#include <iostream>

KickassConfig::KickassConfig(QWidget *parent, const QVariantList &args)
    : KCModule(KickassPluginFactory::componentData(), parent, args)
{
	if (pathKickass.isNull()) {
		pathKickass="";
	}
	
    QVBoxLayout *layout = new QVBoxLayout(this);
    createHorizontalGroupBox();
    
    layout->addWidget(horizontalGroupBox);
    layout->addWidget(formGroupBox);
    layout->addWidget(termGroupBox);
    
    setLayout(layout);
    load();

    // Changed slots
    connect(fileButton,SIGNAL(clicked(bool)),this,SLOT(showFileDialog(bool)));
    connect(path, SIGNAL(textChanged(QString)), this, SLOT(slotPathChanged(QString)));
    connect(wkOptions, SIGNAL(textChanged(QString)), this, SLOT(slotPathChanged(QString)));
    connect(wvOptions, SIGNAL(textChanged(QString)), this, SLOT(slotPathChanged(QString)));
    connect(wtOptions, SIGNAL(textChanged(QString)), this, SLOT(slotPathChanged(QString)));
    connect(termGroupBox,SIGNAL(toggled(bool)),this,SLOT(slotTermChecked(bool)));
}


void KickassConfig::createHorizontalGroupBox()
 {
     horizontalGroupBox = new QGroupBox(tr("Kick Assembler Location"));
     QHBoxLayout *layout = new QHBoxLayout;

     QLabel *lformat = new QLabel(i18n("Path to Kick Assembler"));
     path = new KLineEdit(this);
     //fileWidget = new KFileWidget(KUrl(),this);
     fileButton  = new QPushButton(tr("Find Kick Assembler..."));
     layout->addWidget(lformat);
     layout->addWidget(path);
     layout->addWidget(fileButton);
     horizontalGroupBox->setLayout(layout);
     
     wkOptions = new KLineEdit(this);
     wvOptions = new KLineEdit(this);
     formGroupBox = new QGroupBox(tr("Command line switches"));
     QFormLayout *layout2 = new QFormLayout;
     layout2->addRow(new QLabel(tr("Assembler options:")), wkOptions);
     layout2->addRow(new QLabel(tr("Emulator options:")), wvOptions);
     formGroupBox->setLayout(layout2);
     
     termGroupBox = new QGroupBox(tr("Spawn a terminal (When you need the monitor)"));
     QHBoxLayout *layout3 = new QHBoxLayout;
     wtOptions = new KLineEdit(this);
     layout3->addWidget(wtOptions);
     termGroupBox->setLayout(layout3);
     termGroupBox->setCheckable(true);
 }

void KickassConfig::showFileDialog(bool checked)
{
     Q_UNUSED(checked);
     path->setText(KFileDialog::getOpenFileName());    
}
KickassConfig::~KickassConfig()
{
}

void KickassConfig::save()
{
    if (KickassPlugin::self())
    {
        KickassPlugin::self()->setkOptions(wkOptions->text());
        KickassPlugin::self()->setvOptions(wvOptions->text());
	KickassPlugin::self()->settOptions(wtOptions->text());
	KickassPlugin::self()->setPath(path->text());
	KickassPlugin::self()->setTerminal(termGroupBox->isChecked());
        KickassPlugin::self()->writeConfig();
	//std::cout << "going to self... -> " << qPrintable(path->text()) << std::endl;
    }
    else
    {
        KConfigGroup cg(KGlobal::config(), "Kickass Plugin");
        cg.writeEntry("strkOptions", wkOptions->text());
        cg.writeEntry("strvOptions", wvOptions->text());
	cg.writeEntry("strtOptions", wtOptions->text());
	cg.writeEntry("terminal",termGroupBox->isChecked());
        cg.writeEntry("strPath", path->text());
	//std::cout << "going to config... -> " << qPrintable(path->text()) << std::endl;
    }

    emit changed(false);
}

void KickassConfig::load()
{
    if (KickassPlugin::self())
    {
        KickassPlugin::self()->readConfig();
        wkOptions->setText(KickassPlugin::self()->kOptions());
        wvOptions->setText(KickassPlugin::self()->vOptions());
	wtOptions->setText(KickassPlugin::self()->tOptions());
	termGroupBox->setChecked(KickassPlugin::self()->terminal());
	path->setText(KickassPlugin::self()->path());
	//std::cout << "reading from self()" << std::endl;
    }
    else
    {
	    
        KConfigGroup cg(KGlobal::config(), "Kickass Plugin" );
        wkOptions->setText(cg.readEntry("strkOptions", ""));
        wvOptions->setText(cg.readEntry("strvOptions", ""));
	wtOptions->setText(cg.readEntry("strtOptions", ""));
 	termGroupBox->setChecked(cg.readEntry("terminal",bool(false)));
	
	path->setText(cg.readEntry("strPath", ""));
	//std::cout << "reading from config file" << std::endl;
    }

    emit changed(false);
}

void KickassConfig::defaults()
{
    wkOptions->setText(gkOptions);
    wvOptions->setText(gvOptions);
    wtOptions->setText(gtOptions);
    termGroupBox->setChecked(gTerminal);
    path->setText("");
    

    emit changed(true);
}

void KickassConfig::slotChanged(bool)
{
    emit changed(true);
}

void KickassConfig::slotPathChanged(QString)
{
    emit changed(true);
}

void KickassConfig::slotTermChecked(bool on)
{
    if (on)
	wtOptions->setEnabled(true);
    else
	wtOptions->setEnabled(false);
    emit changed(true);
}

#include "kickass_config.moc"

